package com.xingda.packing.domain;

import com.xingda.packing.enums.AlarmTriggerType;
import com.xingda.packing.enums.BoxLocationNo;
import com.xingda.packing.events.EventCreater;
import com.xingda.packing.plc.service.PlcDirectiveService;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import java.io.Serializable;
import java.util.Objects;

@Slf4j
@Data
public class ProductBoxLocation implements Serializable {

    @Value("${com.xingda.model}")
    @Getter
    private String model;

    private static final long serialVersionUID = -4945739395545466560L;

    /**
     * 位置编号
     */
    private int locationNo;

    public boolean isLeft() {
        return locationNo < 11;
    }

    public boolean isRight() {
        return locationNo > 10;
    }

    /**
     * 拍照是否完成地址点
     */
    private String photoAddress;

    private String palletComing;

    /**
     * 产品包装箱
     */
    private ProductBox productBox;

    PlcDirectiveService plcDirectiveService;
    EventCreater eventCreater;

    public ProductBoxLocation(int locationNo, String photoAddress, String palletComing, PlcDirectiveService plcDirectiveService,
                              EventCreater eventCreater) {
        this.locationNo = locationNo;
        this.photoAddress = photoAddress;
        this.palletComing = palletComing;
        this.plcDirectiveService = plcDirectiveService;
        this.eventCreater = eventCreater;
    }

    /**
     * 位置是否为空（是否有包装箱）
     */
    public boolean isEmpty() {
        return Objects.isNull(productBox);
    }

    public boolean isNotEmpty() {
        return Objects.nonNull(productBox);
    }

    /**
     * 是否可以进行作业
     */
    public boolean canWork() {
        if (isEmpty()) {
            return false;
        }
        return !productBox.isFull();
    }

    public ProductBox getBox() {
        return productBox;
    }

    /**
     * 是否定位拍照
     */
    public boolean isPhotoFinish() {
        return plcDirectiveService.isPhotoFinish(photoAddress);
    }

    public boolean isPalletComing() {
        return !plcDirectiveService.isTrue(palletComing);
    }

    /**
     * 装箱位满报警
     */
    public void triggerFullAlarm(int area) {

//       测试用
        log.info("==== triggerFullAlarm方法下- model:{}, locationNo:{}", model, locationNo);

        if (model.equals("DMSS")) {
            boolean isFull = productBox.isFull();
            int locationNo = this.getLocationNo();
            if (locationNo > 6) {
                locationNo = locationNo - 6;
            }
            if (locationNo == 1) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_1_FULL, isFull, area);
            } else if (locationNo == 2) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_2_FULL, isFull, area);
            } else if (locationNo == 3) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_3_FULL, isFull, area);
            } else if (locationNo == 4) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_4_FULL, isFull, area);
            } else if (locationNo == 5) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_5_FULL, isFull, area);
            } else if (locationNo == 6) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_6_FULL, isFull, area);
            }

        } else if (model.equals("DMDS")) {
            boolean isFull = productBox.isFull();
            int locationNo = this.getLocationNo();
            if (locationNo > 10) {
                locationNo = locationNo - 10;
            }
            if (locationNo == 1) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_1_FULL, isFull, area);
            } else if (locationNo == 2) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_2_FULL, isFull, area);
            } else if (locationNo == 3) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_3_FULL, isFull, area);
            } else if (locationNo == 4) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_4_FULL, isFull, area);
            } else if (locationNo == 5) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_5_FULL, isFull, area);
            } else if (locationNo == 6) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_6_FULL, isFull, area);
            } else if (locationNo == 7) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_7_FULL, isFull, area);
            } else if (locationNo == 8) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_8_FULL, isFull, area);
            } else if (locationNo == 9) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_9_FULL, isFull, area);
            } else if (locationNo == 10) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_10_FULL, isFull, area);
            }
        } else if (model.equals("TMSS")) {

            boolean isFull = productBox.isFull();
            int locationNo = BoxLocationNo.getById(this.getLocationNo()).getNo();
            if (locationNo == 1) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_1_FULL, isFull, area);
            } else if (locationNo == 2) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_2_FULL, isFull, area);
            } else if (locationNo == 3) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_3_FULL, isFull, area);
            } else if (locationNo == 4) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_4_FULL, isFull, area);
            } else if (locationNo == 5) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_5_FULL, isFull, area);
            } else if (locationNo == 6) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_6_FULL, isFull, area);
            } else if (locationNo == 7) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_7_FULL, isFull, area);
            } else if (locationNo == 8) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_8_FULL, isFull, area);
            } else if (locationNo == 9) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_9_FULL, isFull, area);
            } else if (locationNo == 10) {
                eventCreater.createTriggerAlarm(AlarmTriggerType.PRODUCT_BOX_LOCATION_10_FULL, isFull, area);
            }
        }

    }

}
